const arr = [1, 3, 4, 2, 5, 6, 7];

function findIndex(arr) {
    let resArr = new Array(arr.length).fill(-1)
    // 单调递减栈
    let stack = []
    stack.push(0)
    for (let i = 1; i < arr.length; i ++) {
        if (arr[i] < stack[stack.length - 1]) {
            stack.push(i)
        }
        else {
            // 循环出栈
            while (arr[i] > stack[stack.length - 1]) {
                resArr[stack.pop()] = i
            }
            // 出完栈 该这个元素进栈了
            stack.push(i)
        }
    }
    return resArr
}

console.log(findIndex(arr));
